package com.bwc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwc.common.R;
import com.bwc.domain.Category;
import com.bwc.domain.Dish;
import com.bwc.domain.Setmeal;
import com.bwc.domain.SetmealDish;
import com.bwc.dto.DishDto;
import com.bwc.dto.SetmealDto;
import com.bwc.service.CategoryService;
import com.bwc.service.SetmealDishService;
import com.bwc.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;


    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithSetmealDish(setmealDto);

        return R.success("成功增加套餐信息");
    }

    @GetMapping("/page")
    public  R<Page> page(Integer page,Integer pageSize,String name){

        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page,pageSize);


        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,lambdaQueryWrapper);

        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");

        List<Setmeal> setmealList = pageInfo.getRecords();


        List<SetmealDto> setmealDtoList= setmealList.stream().map((item)->{

            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);

            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();

            setmealDto.setCategoryName(categoryName);
            return setmealDto;

        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(setmealDtoList);


        return R.success(setmealDtoPage);
    }

    @GetMapping("{id}")
    public R<SetmealDto> getByIdWith(@PathVariable Long id){
        SetmealDto setmealDto = new SetmealDto();

        Setmeal setmeal = setmealService.getById(id);

        Long categoryId = setmeal.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String categroyName = category.getName();

        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setCategoryName(categroyName);

        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        lambdaQueryWrapper.orderByDesc(SetmealDish::getUpdateTime);

        List<SetmealDish> setmealDishList = setmealDishService.list(lambdaQueryWrapper);
        setmealDto.setSetmealDishes(setmealDishList);

        return R.success(setmealDto);
    }

    @PutMapping
    @Transactional
    public R<String> updateWith(@RequestBody SetmealDto setmealDto){

        //先保存一手父类
        setmealService.updateById(setmealDto);

        //二是删除一手原本的信息
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lambdaQueryWrapper);

        //将得到的新的setmealDish存入表中
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();

        //
        setmealDishList = setmealDishList.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishList);

        return R.success("成功修改套餐信息");
    }

    @DeleteMapping
    public R<String> move(@RequestParam List<Long> ids){
        setmealService.moveWithDish(ids);
        return R.success("成功删除套餐信息");
    }

    @PostMapping("/status/0")
    public R<String> updateStatusDown(@RequestParam List<Long> ids){

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId,ids);

        List<Setmeal> setmealList = setmealService.list(lambdaQueryWrapper);
        setmealList.stream().map((item)->{
            item.setStatus(0);
            return item;
        }).collect(Collectors.toList());

        setmealService.updateBatchById(setmealList);
        return R.success("修改售卖状态成功");
    }

    @PostMapping("/status/1")
    public R<String> updateStatusUp(@RequestParam List<Long> ids){

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId,ids);

        List<Setmeal> setmealList = setmealService.list(lambdaQueryWrapper);
        setmealList.stream().map((item)->{
            item.setStatus(1);
            return item;
        }).collect(Collectors.toList());

        setmealService.updateBatchById(setmealList);
        return R.success("修改售卖状态成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> setmealList = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(setmealList);
    }




}
